//
//  MainTabBarController.swift
//  weibo
//
//  Created by cmh on 15/8/31.
//  Copyright © 2015年 cmh. All rights reserved.
//

import UIKit

class MainTabBarController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 添加所有的控制器
        addAllControllers()
        
    }
    
    // 因为tabar是在将要显示的时候才创建，这个方法中添加加号按钮
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        plusBtnWithFrame()
        
    }
    
    private func plusBtnWithFrame() {
        
        // 获取当前tabbar的个数
        let count = childViewControllers.count
        
        // 计算单个tabarItem的宽度
        let w = tabBar.bounds.width / CGFloat(count)
        
        let rect = CGRectMake(0, 0, w, tabBar.bounds.height)
        
        // 计算加号按钮的frame
        plusBtn.frame = CGRectOffset(rect, 2 * w, 0)
        
//        printLog(plusBtn.frame)
    }
    
    // 添加所有的控制器
    private func addAllControllers() {
        
        addOneController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        
        addOneController(MessageTableViewController(), title: "信息", imageName: "tabbar_message_center")
        
        addChildViewController(UIViewController())
        
        addOneController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        
        addOneController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        
        
    }
    
    // 添加的单个的控制器
    private func addOneController(vc: UIViewController, title: String, imageName: String) {
        
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: imageName)
        
        // 包装成nav控制器
        let nav = UINavigationController(rootViewController: vc)
        
        // 添加控制器
        addChildViewController(nav)
        
 
    }
    
    // 添加tabbar中间的加号图标
    private lazy var plusBtn: UIButton = {
    
        let btn = UIButton()
        
//        btn.backgroundColor = UIColor.redColor()
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
            
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
            
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
            
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        // 添加
        self.tabBar.addSubview(btn)
        
        // 监听按钮的点击事件
        btn.addTarget(self, action: "plusBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    // 让按钮点击的方法既是私有的，又能被运行循环监听到
    @objc private func plusBtnClick() {
        
        let vc: UIViewController
        
        // 判断是否是登陆状态，切换不同的控制器
        if UserAccountViewModel.shareUserAccount.userLogon {
            vc = SendStatusViewController()
        } else {
            vc = OAuthViewController()
        }
        
        let nav = UINavigationController(rootViewController: vc)
        
        presentViewController(nav, animated: true, completion: nil)
    
    }

}
